package com.db.utils;

import com.db.dao.BaseDao;
import com.db.jdbc.JdbcUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xin.ding
 * @date 2020/11/27 17:42
 */
public class BuildDataUtils {
    private static final Logger log = LoggerFactory.getLogger(BuildDataUtils.class);

    /**
     * INSERT INTO T_TEST1 VALUES (%d, 'oracle-test1-c1-%s')
     * <p>
     * 基于某个值动态增加造数
     *
     * @param tableName  插入表名称
     * @param isTruncate 插入数据前是否清空表
     * @param num        数据条数
     */
    public static void buildData(String tableName, String sql, boolean isTruncate, int num) {
        buildData(new BaseDao(), tableName, sql, isTruncate, num);
    }

    public static void buildData(BaseDao baseDao, String tableName, String sql, boolean isTruncate, int num) {
        log.info("start build data for table {},please waite...", tableName);
        String truncateSql = String.format("TRUNCATE TABLE %s", tableName);
        if (isTruncate) {
            baseDao.executeUpdate(truncateSql);
        }
        for (int i = 1; i <= num; i++) {
            String newSql = String.format(sql, i);
            log.debug(newSql);
            baseDao.executeUpdate(newSql);
        }
        log.info("finish build data for table {}", tableName);
    }
}
